﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Traceability_2203.Common.Domain;
using Traceability_2203.Common.Dto;
using Traceability_2203.Common.Infrastructure.interFaces;
using Traceability_2203.Enum;

namespace Traceability_2203.Api.Application.Commands.USers
{
    public class UserSearchHander : IRequestHandler<UserSearchcommand, Reult<List<UsersDto>>>
    {




        private readonly IBaseRepository<Users> _user;
        private readonly IMapper _mapper;

        public UserSearchHander(IBaseRepository<Users> user, IMapper _mapper)
        {
            _user = user;
           this._mapper = _mapper;
        }

        public async Task<Reult<List<UsersDto>>> Handle(UserSearchcommand request, CancellationToken cancellationToken)
        {

            var result = new Reult<List<UsersDto>>  { Code = (int)CommonErrorCode.Added };


            try
            {

                var user = await(await _user.GetAll()).ToListAsync();


                var list=_mapper.Map<List<UsersDto>>(user);

                result.Data = list;

                result.Code = (int)CommonErrorCode.Success;
            }
            catch (Exception ex)
            {
                result.Msg = "修改失败";
                throw;
            }

            return result;



        }

       
    }
}
